package mamma.dao;

import java.util.HashMap;
import java.util.List;

import mamma.common.MybatisConfig;
import mamma.vo.MemberVo;
import mamma.vo.PhotoCommentVo;
import mamma.vo.PhotoVo;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class PhotoDao {
	private SqlSessionFactory factory;
	
	//사진 객체 하나 가져오기
	public PhotoVo getPhoto(PhotoVo vo){
		PhotoVo photo= null;
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = null;
		try {
			session = factory.openSession();
			photo = session.selectOne("photo.getPhoto", vo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return photo;
	}
	// 사진 넣기
	public int insertPhoto(PhotoVo photo){
		int result = 0;
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = null;
		try {
			session = factory.openSession();
			result = session.insert("photo.insertPhoto",photo);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		
		return result;
	}
	//프로필 넣기
	public int insertProfile(PhotoVo photo){
		int result = 0;
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = null;
		try {
			session = factory.openSession();
			//photo에 추가
			session.insert("photo.insertProfile",photo);
			session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		
		return result;
	}
	// savedFile로 No 출력
	public int selectId(String savedFile){
		int result =0;
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = null;
		try {
			session = factory.openSession();
			result = session.selectOne("photo.selectId",savedFile);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		
		return result;
	}
	// mammaMember의 profile_id를 update
	public int memberUpdate(String email,int profile_id){
		int result =0;
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = null;
		
		//member에 no 입력
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("email", email);
		map.put("profile_id", profile_id);
		try {
			session = factory.openSession();
			session.update("photo.changeProfile",map);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}
	
	//  id 값으로 photo에서 savedFile의 값을 가져옴
	public String gtProfile(int id){
		String result = "";
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = null;
		try {
			session = factory.openSession();
			result = session.selectOne("photo.gtProfile",id);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		
		return result;
	}
	//e-mail로 member를 찾음
	public MemberVo getMember(String email){
		MemberVo vo= null;
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = null;
		try {
			session = factory.openSession();
			vo= session.selectOne("photo.getMember",email);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		
		return vo;
	}
	/* member의 정보를 Update*/
	public int updateProfile(MemberVo memberVo){
		int result = 0;
		System.out.println(memberVo);
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = null;
		try {
			session = factory.openSession();
			result= session.update("photo.updateProfile",memberVo);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		
		return result;
	}
	
	/* email에 속한 전체 photo 갯수*/
	public int totalPhoto(String email){
		int result =0;
		
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = factory.openSession();
		result = session.selectOne("photo.totalPhoto",email);
		
		session.close();
		return result;
	}
	/* email에 속한 전체 photo객체의 savedFile 과 id 를 가져옴*/
	public List<PhotoVo> photoList(String email, int startRecord,int countPerPage){
		List<PhotoVo> list=null;
		/* 대용량의 경우 느림*/
		PhotoVo vo = new PhotoVo();
		vo.setEmail(email);
		vo.setStartRecord(startRecord);
		vo.setCountPerPage(countPerPage);
		
		System.out.println("startRecord:" + startRecord);
		System.out.println("countPerPage:" + countPerPage);

		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = factory.openSession();
		list = session.selectList("photo.photoList", vo);
		System.out.println(list);
		session.close();
		
		return list;
	}
	
	public List<PhotoCommentVo> selectPhotoComment(PhotoCommentVo vo){
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = factory.openSession();
		List<PhotoCommentVo> list = session.selectList("photo.selectPhotoComment", vo);
		session.close();
		return list;
	}
	
	public int insertPhotoComment(PhotoCommentVo vo){
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = factory.openSession();
		
		int result = session.insert("photo.insertPhotoComment", vo);
		session.commit();
		session.close();
		
		return result;
	}
	
	public int likeit(int id){
		factory = MybatisConfig.getSqlSessionFactory();
		SqlSession session = factory.openSession();
		
		int result = session.insert("photo.likeit", id);
		session.commit();
		session.close();
		
		return result;
	}
}
